package cn.edu.hstc.wm.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import cn.edu.hstc.wm.AppApplication;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.ui.fragment.MyComment;
import cn.edu.hstc.wm.ui.fragment.MyInfo;
import cn.edu.hstc.wm.ui.fragment.MyOrder;
import cn.edu.hstc.wm.widget.CustomToast;
import cn.edu.hstc.wm.widget.PagerSlideTab;

/**
 * 用户中心
 * 
 */
public class UCenter extends FragmentActivity {

	private static final String TAG = UCenter.class.getName();


	private PagerSlideTab mPagerTab;
	private ViewPager mPager;
	private UCenterAdapter mAdapter;
	private List<Fragment> mFragments;
	private final String[] TAB_TITLES = { "我的信息", "我的收藏", "我的评价" };

	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.ucenter);

		initActionbar();
		initViewPager();
		initPagerTab();
	}

	private void initActionbar() {
		
		 ImageButton backView = (ImageButton) findViewById(R.id.ib_actionbar_back);
		 TextView titleView = (TextView) findViewById(R.id.tv_actionbar_title);
		 backView.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		 titleView.setText("用戶中心");
		 ImageButton logoutButton = (ImageButton) findViewById(R.id.ib_actionbar_share);
		 logoutButton.setImageResource(R.drawable.ic_logout);
		 logoutButton.setVisibility(View.VISIBLE);
		 logoutButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				final AppApplication app = (AppApplication) UCenter.this.getApplication();
				AlertDialog.Builder builder = new Builder(UCenter.this);
				builder.setTitle("注销登录").setMessage("真的要注销登录?").setPositiveButton("是的", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						app.logout();
						finish();
						CustomToast.make(UCenter.this).setText("已退出登录").show();
					}
				}).setNegativeButton("不", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create().show();
			
			}
		});
	}

	private void initPagerTab() {
		mPagerTab = (PagerSlideTab) findViewById(R.id.ucenter_pager_slide_tab);
		// 设置其他属性
		// mPagerTab.setShouldExpand(true);
		mPagerTab.setViewPager(mPager);
	}

	private void initViewPager() {
		mFragments = new ArrayList<Fragment>();
		// TODO 添加内容
		mFragments.add(new MyInfo());
		mFragments.add(new MyOrder());
		mFragments.add(new MyComment());

		mPager = (ViewPager) findViewById(R.id.ucenter_viewpager);
		mAdapter = new UCenterAdapter(getSupportFragmentManager());
		mPager.setAdapter(mAdapter);
	}

	private final class UCenterAdapter extends FragmentPagerAdapter {

		public UCenterAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			return mFragments.get(arg0);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return TAB_TITLES[position];
		}

	}
}
